﻿using System;
using System.Windows.Forms;

namespace Skylabs.Breeze
{
    public partial class frmSettings : Form
    {
        public frmSettings()
        {
            InitializeComponent();
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            textBox1.Text = Breeze.Properties.Settings.Default.dod;
            checkBox2.Checked = Properties.Settings.Default.showserverwindowonstart;
            checkBox1.Checked = GetStartup();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            SetStartup(checkBox1.Checked);
            Breeze.Properties.Settings.Default.dod = textBox1.Text;
            Properties.Settings.Default.showserverwindowonstart = checkBox2.Checked;
            Properties.Settings.Default.Save();
            this.Close();
        }
        private void SetStartup(bool enable)
        {
            string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

            Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);

            if (enable)
            {
                if (startupKey.GetValue(Application.ProductName) == null)
                {
                    startupKey.Close();
                    startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                    // Add startup reg key
                    startupKey.SetValue(Application.ProductName, Application.ExecutablePath.ToString());
                    startupKey.Close();
                }
            }
            else
            {
                // remove startup
                if (startupKey.GetValue(Application.ProductName) != null)
                {
                    startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                    startupKey.DeleteValue(Application.ProductName, false);
                    startupKey.Close();
                }
            }
        }
        private bool GetStartup()
        {
            string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
            Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);
            return startupKey.GetValue(Application.ProductName) != null;
        }

        private void frmSettings_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.SettingsWindow = null;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            
        }

        private void btCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
